package com.shotgun.my.service.serviceImpl.defaultGroup.subGroup;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shotgun.my.api.api.defaultGroup.subGroup.BallServiceApi;
import com.shotgun.my.api.po.pojos.defaultGroup.subGroup.Ball;
import com.shotgun.my.service.base.AbstractCommonService;
import com.shotgun.my.service.dao.defaultGroup.subGroup.BallMapper;
import com.shotgun.my.service.service.defaultGroup.subGroup.BallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shotgun.my.sweetutil.time.DateUtils;
import shotgun.my.sweetutil.time.TimeUtil;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wulm
 * 当service方法和ServiceApi方法有重载方法时，应当用api的实现方法调用service的实现方法，具体验证逻辑写在service中，相当于serviceApi是controller，封装更多。
 **/
@RestController
@RequestMapping(BallServiceApi.PATH)
public class BallServiceImpl extends AbstractCommonService<BallMapper, Ball> implements BallService {

    @Autowired
    private BallMapper ballMapper;


//    public static void main(String[] args) {
//        new BallServiceImpl().test();
//    }

    public static final String TYPE_PRE_STR = "fucai-";


    @PostConstruct
    public void test() {

        String workDir = "D:/zzfucai/2025-06-28/";
        String fileType = ".json";

//        List<String> ilist = new ArrayList<>();
//        for (int i = 1; i <= 63; i++) {
//            ilist.add(String.valueOf(i));
////            System.out.println(i);
//        }
//        List<String> fileStrList = ilist;

        List<String> fileStrList = Arrays.asList("1");

        List<String> paths = fileStrList.stream().map(file -> workDir + file + fileType).collect(Collectors.toList());
        paths.forEach(s -> {
            extracted(s);
        });


        System.out.println("执行完毕 end...");

    }

    private void extracted(String path) {
        String fileStr = FileUtil.readUtf8String(path);

        JSONObject jsonObject = JSON.parseObject(fileStr);
        //每期数据
        JSONArray result = jsonObject.getJSONArray("result");
        if (result == null || result.size() == 0) {
            throw new IllegalArgumentException("无数据");
        }
        for (int i = 0; i < result.size(); i++) {
            JSONObject joi = result.getJSONObject(i);
            System.out.println(joi.toJSONString());

            if (!"双色球".equals(joi.getString("name"))) {
                throw new RuntimeException("数据有误!!");
            }

            String date = joi.getString("date").substring(0, 10);
            String code = joi.getString("code");
            List<Integer> red = Stream.of(joi.getString("red").split(",")).map(Integer::parseInt).collect(Collectors.toList());
            List<Integer> blue = Stream.of(joi.getString("blue").split(",")).map(Integer::parseInt).collect(Collectors.toList());

            System.out.println("date=" + date + " code=" + code + " red=" + red + " blue=" + blue);

            Integer count = ballMapper.selectCount(new LambdaQueryWrapper<Ball>()
                    //期数已存在
                    .eq(Ball::getPeriods, code)
            );

            if (count > 0) {
                System.out.println("期数已存在:" + code + ",不处理，继续执行后续逻辑.");
                continue;
            }
//            if (count > 0) {
//                System.out.println("期数已存在:" + code + ",删除后重新插入.");
//                ballMapper.delete(new LambdaQueryWrapper<Ball>()
//                        .eq(Ball::getPeriods, code)
//                );
//            }

            List<Ball> ballReds = red.stream().map(redNum -> {
                Ball ball = new Ball();
                ball.setRunDate(DateUtils.UTIL_LOCAL_DATE_TIME.parse(date, TimeUtil.YYYY_MM_DD).toLocalDate());
                ball.setPeriods(code);
                ball.setNumber(redNum);
                ball.setType(TYPE_PRE_STR + "red");
                return ball;
            }).collect(Collectors.toList());

            List<Ball> ballBlues = blue.stream().map(blueNum -> {
                Ball ball = new Ball();
                ball.setRunDate(DateUtils.UTIL_LOCAL_DATE_TIME.parse(date, TimeUtil.YYYY_MM_DD).toLocalDate());
                ball.setPeriods(code);
                ball.setNumber(blueNum);
                ball.setType(TYPE_PRE_STR + "blue");
                return ball;
            }).collect(Collectors.toList());

            List<Ball> ballAlls = new ArrayList<>(ballBlues.size() + ballReds.size());
            if (ballReds.isEmpty()) {
                System.out.println("无球!!!");
            } else {
                ballAlls.addAll(ballReds);
                ballAlls.addAll(ballBlues);
            }

            ballAlls.forEach(ball -> {
                ballMapper.insert(ball);
            });


        }
    }


}
